/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/JSP_Servlet/Servlet.java to edit this template
 */
package cn.edu.just.secondwebapp.servlet;

import cn.edu.just.secondwebapp.dao.PersonDao;
import cn.edu.just.secondwebapp.daoimpl.PersonDaoImpl;
import cn.edu.just.secondwebapp.entity.Person;
import cn.edu.just.secondwebapp.service.PersonService;
import cn.edu.just.secondwebapp.serviceimpl.PersonServiceImpl;
import cn.edu.just.secondwebapp.util.DatabaseConnectionUtil;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author JUST
 */
@WebServlet(name = "AddPersonServlet", urlPatterns = {"/servlet/AddPersonServlet"})
public class AddPersonServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
           
            request.setCharacterEncoding("utf-8");
            
            String name = request.getParameter("name");
            String age = request.getParameter("age");
            Integer ageInt =  Integer.parseInt(age);
            
            String birthday = request.getParameter("birthday");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date birthdayDate = sdf.parse(birthday);
            
            String salary = request.getParameter("gongzi");
            BigDecimal sal = new BigDecimal(salary);            
            Person p = new Person(null,name,ageInt,birthdayDate,sal);
            DatabaseConnectionUtil dcu = new DatabaseConnectionUtil();
            PersonDao personDao = new PersonDaoImpl(dcu.getConnection());
            PersonService personService = new PersonServiceImpl(personDao);
            boolean success = personService.addPerson(p);
            if(success){
                RequestDispatcher rd = request.getRequestDispatcher("../jsp/person02/ShowAllPersons.jsp");
                rd.forward(request, response);
            }else{
                response.sendRedirect("../jsp/main/error.jsp");
            }           
           
        }catch(Exception e){ 
            e.printStackTrace();
            response.sendRedirect("../jsp/main/error.jsp");
           
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
